package com.ljc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljc.annotation.AuthCheck;
import com.ljc.common.BaseResponse;
import com.ljc.common.ErrorCode;
import com.ljc.common.ResultUtils;
import com.ljc.constant.RedisConstant;
import com.ljc.constant.UserConstant;
import com.ljc.exception.BusinessException;
import com.ljc.model.entity.InterfaceInfo;
import com.ljc.model.vo.InterfaceInfoVO;
import com.ljc.model.vo.OrderVO;
import com.ljc.service.InterfaceInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 分析控制器
 */
@RestController
@RequestMapping("/analysis")
@Slf4j
public class AnalysisController {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private InterfaceInfoService interfaceInfoService;

    @GetMapping("/top/interface/invoke")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<List<InterfaceInfoVO>> listTopInvokeInterfaceInfo() {
        Set<String> interfaceIds = stringRedisTemplate.opsForZSet().reverseRange(RedisConstant.ANALYSIS_INVOKE_INTERFACE, 0, 4);
        if (CollectionUtils.isEmpty(interfaceIds)) {
            return ResultUtils.success(Collections.emptyList());
        }
        QueryWrapper<InterfaceInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", interfaceIds);
        List<InterfaceInfo> list = interfaceInfoService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        List<InterfaceInfoVO> interfaceInfoVOList = list.stream().map(interfaceInfo -> {
            InterfaceInfoVO interfaceInfoVO = new InterfaceInfoVO();
            BeanUtils.copyProperties(interfaceInfo, interfaceInfoVO);
            double totalNum = stringRedisTemplate.opsForZSet().score(RedisConstant.ANALYSIS_INVOKE_INTERFACE, String.valueOf(interfaceInfo.getId()));
            interfaceInfoVO.setTotalNum((int) totalNum);
            return interfaceInfoVO;
        }).collect(Collectors.toList());
        return ResultUtils.success(interfaceInfoVOList);
    }

    @GetMapping("/top/interface/buy")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<List<OrderVO>> listTopBuyInterfaceInfo() {
        Set<String> interfaceIds = stringRedisTemplate.opsForZSet().reverseRange(RedisConstant.ANALYSIS_BUY_INTERFACE, 0, 4);
        List<OrderVO> orderVOList = interfaceIds.stream().map(interfaceId -> {
            InterfaceInfo interfaceInfo = interfaceInfoService.getById(interfaceId);
            OrderVO orderVO = new OrderVO();
            orderVO.setInterfaceId(Long.valueOf(interfaceId));
            double totalNum = stringRedisTemplate.opsForZSet().score(RedisConstant.ANALYSIS_BUY_INTERFACE, String.valueOf(interfaceInfo.getId()));
            orderVO.setTotal((long) totalNum);
            orderVO.setInterfaceName(interfaceInfo.getName());
            orderVO.setInterfaceDesc(interfaceInfo.getDescription());
            return orderVO;
        }).collect(Collectors.toList());
        return ResultUtils.success(orderVOList);
    }

}
